#include<iostream>
#include<vector>

using namespace std;

struct ListNode {
	int val;
	ListNode *next;
	ListNode() : val(0), next(nullptr) {}
	ListNode(int x) : val(x), next(nullptr) {}
	ListNode(int x, ListNode *next) : val(x), next(next) {}
	
};

class Solution {
public:
	ListNode* oddEvenList(ListNode* head) {
		if (!head) return NULL;
		ListNode *t = head;
		vector<ListNode *> list;
		list.push_back(NULL);
		while (t) {
			list.push_back(t);
			t = t->next;
		}
		ListNode *odd=NULL, *even = NULL,*oddhead=NULL;
		for (int i = 1; i <= list.size()-1; i++) {
			if (i % 2 ) {
				if (even) {
					even->next = list[i];
					even = even->next;
				}
				else
					even = list[i];
			}
			else {
				if (odd) {
					odd->next = list[i];
					odd = odd->next;
				}
				else {
					odd = list[i];
					oddhead = list[i];
				}
			}
		}
		if (odd) {
			odd->next = NULL;
		}
		even->next = oddhead;
		return head;
	}
};


void main() {
	ListNode n1(1), n2(2), n3(3), n4(4), n5(5);
	//n1.next = &n2, n2.next = &n3, n3.next = &n4, n4.next = &n5;
	Solution s;
	s.oddEvenList(&n1);
}